package Call_2;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class GetName {
    static Random R = new Random();
    public static void main(String[] args) throws IOException {
        String strXing="https://hanyu.baidu.com/shici/detail?from=kg1&highlight=&pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&srcid=51369";
        String strMing="http://www.haoming8.cn/mingzi/8785.html";
        String strXingURL = webCarawaler(strXing);
        String strMingURL = webCarawaler(strMing);
        ArrayList<String> strXingURList=getData(strXingURL,".{4}，.{4}。");
        ArrayList<String> strMingURList=getData(strMingURL,"(..、){4}..");
        ArrayList<String> strXingD=getXingDataUrl(strXingURList);
        ArrayList<String> strMingD=getMingDataUrl(strMingURList);
        ArrayList<String> XM=getMan(strXingD,strMingD,100);
        System.out.println(XM);
        FileWriter out=new FileWriter("namesboyandgils.txt");
        BufferedWriter bw=new BufferedWriter(out);
        for (String string : XM) {
            bw.write(string);
            bw.newLine();
        }
        bw.close();
    }




    private static ArrayList<String> getMan(ArrayList<String> strXingD, ArrayList<String> strMingD, int count) {
        HashSet<String> hashset=new HashSet<>();
        while(true){
            if(count==hashset.size()){
                break;
            }
            //随机
            Collections.shuffle(strXingD);
            Collections.shuffle(strMingD);
            if(hashset.size()>50){
                int ages = R.nextInt(6)+20;
                hashset.add(strXingD.get(0)+strMingD.get(0)+"-男"+"-"+ages);
            }else{
                int age = R.nextInt(5)+26;
                hashset.add(strXingD.get(0)+strMingD.get(0)+"-女"+"-"+age);
            }

        }
        ArrayList<String> list=new ArrayList<>(hashset);
        return list;
    }

    private static ArrayList<String> getMingDataUrl(ArrayList<String> strMingURList) {
        ArrayList<String> list=new ArrayList<>();
        for (String string : strMingURList) {
            String[] split = string.split("、");
            for (String string2 : split) {
                list.add(string2);
            }
        }
        return list;
    }



    private static ArrayList<String> getXingDataUrl(ArrayList<String> strXingURList) {
        ArrayList<String> list=new ArrayList<String>();
        for (String string : strXingURList) {
            if(string!=null){
                string=string.substring(0, string.length()-1);
                //string 赵钱孙李，周吴郑王
                String[] split = string.split("，");
                // 赵钱孙李    周吴郑王。
                for (String str : split) {
                    for(int i=0;i<str.length();i++){
                        char c=str.charAt(i);
                        list.add(c+"");
                    }
                }
            }
        }
        return list;
    }


    public static String webCarawaler(String url) throws IOException{
        StringBuilder sb=new StringBuilder();
        InputStream inputStream =
                new URL(url)
                        .openConnection()
                        .getInputStream();
        //读取数据
        InputStreamReader isr=new InputStreamReader(inputStream,"utf-8");
        int ch;
        while((ch=isr.read())!=-1){
            sb.append((char)ch);
        }
        isr.close();
        return sb.toString();
    }

    private static ArrayList<String> getData(String strXingURL, String regex) {
        ArrayList<String> list=new ArrayList<String>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(strXingURL);
        while(matcher.find()){
            String group = matcher.group();
            list.add(group);
        }
        return list;
    }















}
